package cn.lngaosu.lwyd.module.integration.controller.admin.toutiaoaccount;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.lngaosu.lwyd.framework.common.pojo.PageParam;
import cn.lngaosu.lwyd.framework.common.pojo.PageResult;
import cn.lngaosu.lwyd.framework.common.pojo.CommonResult;
import cn.lngaosu.lwyd.framework.common.util.object.BeanUtils;
import static cn.lngaosu.lwyd.framework.common.pojo.CommonResult.success;

import cn.lngaosu.lwyd.framework.excel.core.util.ExcelUtils;

import cn.lngaosu.lwyd.framework.apilog.core.annotation.ApiAccessLog;
import static cn.lngaosu.lwyd.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.lngaosu.lwyd.module.integration.controller.admin.toutiaoaccount.vo.*;
import cn.lngaosu.lwyd.module.integration.dal.dataobject.toutiaoaccount.ToutiaoAccountDO;
import cn.lngaosu.lwyd.module.integration.service.toutiaoaccount.ToutiaoAccountService;

@Tag(name = "管理后台 - 今日头条账号基本信息")
@RestController
@RequestMapping("/integration/toutiao-account")
@Validated
public class ToutiaoAccountController {

    @Resource
    private ToutiaoAccountService toutiaoAccountService;

    @PostMapping("/create")
    @Operation(summary = "创建今日头条账号基本信息")
    @PreAuthorize("@ss.hasPermission('integration:toutiao-account:create')")
    public CommonResult<Long> createToutiaoAccount(@Valid @RequestBody ToutiaoAccountSaveReqVO createReqVO) {
        return success(toutiaoAccountService.createToutiaoAccount(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新今日头条账号基本信息")
    @PreAuthorize("@ss.hasPermission('integration:toutiao-account:update')")
    public CommonResult<Boolean> updateToutiaoAccount(@Valid @RequestBody ToutiaoAccountSaveReqVO updateReqVO) {
        toutiaoAccountService.updateToutiaoAccount(updateReqVO);
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除今日头条账号基本信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('integration:toutiao-account:delete')")
    public CommonResult<Boolean> deleteToutiaoAccount(@RequestParam("id") Long id) {
        toutiaoAccountService.deleteToutiaoAccount(id);
        return success(true);
    }

    @GetMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除今日头条账号基本信息")
                @PreAuthorize("@ss.hasPermission('integration:toutiao-account:delete')")
    public CommonResult<Boolean> deleteToutiaoAccountList(@RequestParam("ids") List<Long> ids) {
        toutiaoAccountService.deleteToutiaoAccountListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得今日头条账号基本信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('integration:toutiao-account:query')")
    public CommonResult<ToutiaoAccountRespVO> getToutiaoAccount(@RequestParam("id") Long id) {
        ToutiaoAccountDO toutiaoAccount = toutiaoAccountService.getToutiaoAccount(id);
        return success(BeanUtils.toBean(toutiaoAccount, ToutiaoAccountRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得今日头条账号基本信息分页")
    @PreAuthorize("@ss.hasPermission('integration:toutiao-account:query')")
    public CommonResult<PageResult<ToutiaoAccountRespVO>> getToutiaoAccountPage(@Valid ToutiaoAccountPageReqVO pageReqVO) {
        PageResult<ToutiaoAccountDO> pageResult = toutiaoAccountService.getToutiaoAccountPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ToutiaoAccountRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出今日头条账号基本信息 Excel")
    @PreAuthorize("@ss.hasPermission('integration:toutiao-account:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportToutiaoAccountExcel(@Valid ToutiaoAccountPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ToutiaoAccountDO> list = toutiaoAccountService.getToutiaoAccountPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "今日头条账号基本信息.xls", "数据", ToutiaoAccountRespVO.class,
                        BeanUtils.toBean(list, ToutiaoAccountRespVO.class));
    }

}